home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / BIRTHSRC.ZIP / FIXED.INC < prev    next >
Encoding:
Text File  |  1993-11-23  |  8.8 KB  |  349 lines

  1. ;╔════════════════════════════════════════════════╗
  2. ;║  Name:Fixed_Point_Routines____________  V01.00 ║
  3. ;║                                                ║
  4. ;║  Modified:15-Nov-93                         FB ║
  5. ;╚════════════════════════════════════════════════╝
  6. ;
  7.  
  8. ;**************************************************
  9.  
  10. INCLUDE TRIGTABL.DAT
  11.  
  12. ;--------------------------------------------------
  13. ; FixedMul - Fixed Point Multiplication and rounding
  14. ;    IN     : - eax
  15. ;               edx
  16. ;   OUT     : - eax
  17. ;  Modified : eax,edx
  18. ;  Vars Used: -
  19.  
  20. FixedMul        PROC
  21.  
  22.     imul    edx
  23.     add     eax,8000h
  24.     adc     edx,0
  25.     shrd    eax,edx,16
  26.     ret
  27.  
  28.  
  29. FixedMul        ENDP
  30. ;--------------------------------------------------
  31. ; FixedDiv - Fixed Point Division (No Rounding!)
  32. ;    IN     : - edx     (edx div ebx)
  33. ;               ebx
  34. ;   OUT     : - eax
  35. ;  Modified : eax,ebx,edx
  36.  
  37. FixedDiv        PROC
  38.  
  39.        ;mov     edx,A
  40.     xor     eax,eax
  41.     shrd    eax,edx,16
  42.     sar     edx,16
  43.     idiv    ebx
  44.     ret
  45.  
  46. FixedDiv        ENDP
  47. ;--------------------------------------------------
  48. ; Sin - Fixed Point Sine (lookup - circle=1024 steps)
  49. ;    IN     : bx  (deg 0..1023)
  50. ;   OUT     : eax
  51. ;  Modified : bx dx
  52. Sin            PROC
  53.  
  54.        ;mov     bx,DEG          ;0-> 1, 256-> 0, 512-> -1, 768-> 0
  55.     add     bx,768          ;convert sin to cos
  56.     xor     dx,dx
  57.     and     bx,1023         ;Maxdegree
  58.     shl     bx,1
  59.     mov     ax,cs:trigtable[bx]
  60.     cmp     bx,256*2          ;is it negative
  61.     jbe     fcosi2
  62.     cmp     bx,768*2
  63.     jae     fcosi2
  64.     dec     dx
  65. fcosi2:
  66.     shl     eax,16
  67.     shrd    eax,edx,16
  68.     ret
  69.  
  70. Sin            ENDP
  71. ;--------------------------------------------------
  72. ; Cos - Fixed Point Cosine (lookup - circle=1024 steps)
  73. ;    IN     : bx  (deg 0..1023)
  74. ;   OUT     : eax
  75. ;  Modified : bx dx
  76. Cos            PROC
  77.  
  78.        ;mov     bx,DEG          ;0-> 1, 256-> 0, 512-> -1, 768-> 0
  79.     xor     dx,dx
  80.     and     bx,1023         ;Maxdegree
  81.     shl     bx,1
  82.     mov     ax,cs:trigtable[bx]
  83.     cmp     bx,256*2          ;is it negative
  84.     jbe     fcosi
  85.     cmp     bx,768*2
  86.     jae     fcosi
  87.     dec     dx
  88. fcosi:
  89.     shl     eax,16
  90.     shrd    eax,edx,16
  91.     ret
  92.  
  93. Cos            ENDP
  94. ;--------------------------------------------------
  95. ; SqrtLP - Fixed Point Square Root (Low Precision)
  96. ;    IN     : ecx
  97. ;   OUT     : eax
  98. ;  Modified : ebx,ecx,edx
  99.  
  100. SqrtLP           PROC
  101. ;NOTE:
  102. ;this function only gives a 8.8 precision !!!!!!!!!!!!!
  103.  
  104.     xor     eax,eax
  105.  
  106.     mov     ebx,40000000h
  107. sqrtLP1:
  108.     mov     edx,ecx         ;edx = val
  109.     sub     edx,ebx         ;val - bitsqr
  110.     jl      sqrtLP2
  111.     sub     edx,eax         ;val - root
  112.     jl      sqrtLP2
  113.     mov     ecx,edx         ;val >= (root+bitsqr) -> accept subs
  114.     shr     eax,1           ;root >> 1
  115.     or      eax,ebx         ;root | bitsqr
  116.     shr     ebx,2           ;bitsqr>>2
  117.     jnz     sqrtLP1
  118.     shl     eax,8
  119.     ret
  120. sqrtLP2:
  121.     shr     eax,1           ;val < (root+bitsqr) -> dont change val
  122.     shr     ebx,2           ;bitsqr>>2
  123.     jnz     sqrtLP1
  124.     shl     eax,8
  125.     ret
  126.  
  127. SqrtLP           ENDP
  128. ;--------------------------------------------------
  129. ; Sqrt - Fixed Point Square Root (High/Normal Precision)
  130. ;    IN     : ecx
  131. ;   OUT     : eax
  132. ;  Modified : ebx,ecx,edx
  133. Sqrt         PROC
  134.  
  135. ;This is the High Precision version for the sqrt.
  136. ;It gives the optimal 8.16 precision but takes
  137. ;a little longer (24 iterations, 48 bits intead of
  138. ;16 iterations and 32 bits)
  139.  
  140.     xor     eax,eax         ;eax is root
  141.     mov     ebx,40000000h
  142. sqrt1:
  143.     mov     edx,ecx         ;edx = val
  144.     sub     edx,ebx         ;val - bitsqr
  145.     jb      sqrt2
  146.     sub     edx,eax         ;val - root
  147.     jb      sqrt2
  148.     mov     ecx,edx         ;val >= (root+bitsqr) -> accept subs
  149.     shr     eax,1           ;root >> 1
  150.     or      eax,ebx         ;root | bitsqr
  151.     shr     ebx,2           ;bitsqr>>2
  152.     jnz     sqrt1
  153.     jz      sqrt5
  154. sqrt2:
  155.     shr     eax,1           ;val < (root+bitsqr) -> dont change val
  156.     shr     ebx,2           ;bitsqr>>2
  157.     jnz     sqrt1
  158. ; we now have the 8.8 precision
  159.  
  160. sqrt5:
  161.     mov     ebx,00004000h
  162.     shl     eax,16
  163.     shl     ecx,16
  164. sqrt3:
  165.     mov     edx,ecx         ;edx = val
  166.     sub     edx,ebx         ;val - bitsqr
  167.     jb      sqrt4
  168.     sub     edx,eax         ;val - root
  169.     jb      sqrt4
  170.     mov     ecx,edx         ;val >= (root+bitsqr) -> accept subs
  171.     shr     eax,1           ;root >> 1
  172.     or      eax,ebx         ;root | bitsqr
  173.     shr     ebx,2           ;bitsqr>>2
  174.     jnz     sqrt3
  175.     ret
  176. sqrt4:
  177.     shr     eax,1           ;val < (root+bitsqr) -> dont change val
  178.     shr     ebx,2           ;bitsqr>>2
  179.     jnz     sqrt3
  180.     ret
  181.  
  182. Sqrt           ENDP
  183. ;--------------------------------------------------
  184. ; RotateX - Fixed Point X-Axis-rotation
  185. ;    IN     : gs:si     (ptr to x,y,z fixed point coords)
  186. ;             Xsin      ( sine )
  187. ;             Xcos      ( cosine )
  188. ;   OUT     :
  189. ;             ebx       (y)
  190. ;             ecx       (z)
  191. ;  Modified : eax,edx,ebp
  192. RotateX         PROC
  193.  
  194.     mov     eax,gs:[si+4]       ;get y value
  195.     imul    Xcos                ; y*cos
  196.     add     eax,8000h
  197.     adc     edx,0
  198.     shrd    eax,edx,16
  199.     mov     ebp,eax
  200.  
  201.     mov     eax,gs:[si+8]       ;get z value
  202.     imul    Xsin                ; z*sin
  203.     add     eax,8000h
  204.     adc     edx,0
  205.     shrd    eax,edx,16
  206.     sub     ebp,eax             ; y*cos-z*sin
  207.  
  208.     mov     eax,gs:[si+4]       ;get y value
  209.     imul    Xsin                ; y*sin
  210.     add     eax,8000h
  211.     adc     edx,0
  212.     shrd    eax,edx,16
  213.     mov     ebx,ebp             ;set up return register (y)
  214.     mov     ebp,eax
  215.  
  216.     mov     eax,gs:[si+8]       ;get z value
  217.     imul    Xcos                ; z*cos
  218.     add     eax,8000h
  219.     adc     edx,0
  220.     shrd    eax,edx,16
  221.     add     ebp,eax             ; y*sin+z*cos
  222.     mov     ecx,ebp             ;set up return register (z)
  223.  
  224.     ret
  225.  
  226. RotateX         ENDP
  227. ;--------------------------------------------------
  228. ; RotateY - Fixed Point Y-Axis-rotation
  229. ;    IN     : gs:si     (ptr to x,y,z fixed point coords)
  230. ;             Ysin      ( sine )
  231. ;             Ycos      ( cosine )
  232. ;   OUT     :
  233. ;             ebx       (x)
  234. ;             ecx       (z)
  235. ;  Modified : eax,edx,ebp
  236. RotateY         PROC
  237.  
  238.     mov     eax,gs:[si]         ;get x value
  239.     imul    Ycos                ; x*cos
  240.     add     eax,8000h
  241.     adc     edx,0
  242.     shrd    eax,edx,16
  243.     mov     ebp,eax
  244.  
  245.     mov     eax,gs:[si+8]       ;get z value
  246.     imul    Ysin                ; z*sin
  247.     add     eax,8000h
  248.     adc     edx,0
  249.     shrd    eax,edx,16
  250.     sub     ebp,eax             ; x*cos-z*sin
  251.  
  252.     mov     eax,gs:[si+8]       ;get z value
  253.     imul    Ycos                ; z*cos
  254.     add     eax,8000h
  255.     adc     edx,0
  256.     shrd    eax,edx,16
  257.     mov     ebx,ebp             ;set up return register (x)
  258.     mov     ebp,eax             ; z*cos
  259.  
  260.     mov     eax,gs:[si]         ;get x value
  261.     imul    Ysin                ; x*sin
  262.     add     eax,8000h
  263.     adc     edx,0
  264.     shrd    eax,edx,16
  265.     add     ebp,eax             ; z*cos+x*sin
  266.     mov     ecx,ebp             ;set up return register (z)
  267.  
  268.     ret
  269.  
  270. RotateY         ENDP
  271. ;--------------------------------------------------
  272. ; RotateZ - Fixed Point Z-Axis-rotation
  273. ;    IN     : gs:si     (ptr to x,y,z fixed point coords)
  274. ;             Zsin      ( sine )
  275. ;             Zcos      ( cosine )
  276. ;   OUT     :
  277. ;             ebx       (x)
  278. ;             ecx       (y)
  279. ;  Modified : eax,edx,ebp
  280. RotateZ         PROC
  281.  
  282.     mov     eax,gs:[si]         ;get x value
  283.     imul    Zcos                ; x*cos
  284.     add     eax,8000h
  285.     adc     edx,0
  286.     shrd    eax,edx,16
  287.     mov     ebp,eax
  288.  
  289.     mov     eax,gs:[si+4]       ;get y value
  290.     imul    Zsin                ; y*sin
  291.     add     eax,8000h
  292.     adc     edx,0
  293.     shrd    eax,edx,16
  294.     sub     ebp,eax             ; x*cos-y*sin
  295.  
  296.     mov     eax,gs:[si]         ;get x value
  297.     imul    Zsin                ; x*sin
  298.     add     eax,8000h
  299.     adc     edx,0
  300.     shrd    eax,edx,16
  301.     mov     ebx,ebp             ;set up return register (x)
  302.     mov     ebp,eax
  303.  
  304.     mov     eax,gs:[si+4]       ;get y value
  305.     imul    Zcos                ; y*cos
  306.     add     eax,8000h
  307.     adc     edx,0
  308.     shrd    eax,edx,16
  309.     add     ebp,eax             ; x*sin+y*cos
  310.     mov     ecx,ebp             ;set up return register (y)
  311.  
  312.     ret
  313.  
  314. RotateZ         ENDP
  315. ;--------------------------------------------------
  316. ; Three2TwoD - Fixed Point (x,y,z) to (x,y)
  317. ;    IN     : gs:si     (ptr to x,y,z fixed point coords)
  318. ;             ebx       (z) value  (usually gs:[si+8])
  319. ;   OUT     :
  320. ;             ecx       (x)
  321. ;             edx       (y)
  322. ;  Modified : eax
  323. Three2TwoD      PROC
  324.  
  325.     mov     edx,gs:[si+4]           ;get y-value
  326.     shl     edx,7                   ;zoom factor = 128
  327.  
  328.     xor     eax,eax
  329.     shrd    eax,edx,16
  330.     sar     edx,16
  331.     idiv    ebx
  332.     mov     ecx,eax
  333.  
  334.     mov     edx,gs:[si]             ;get x-value
  335.     shl     edx,7                   ;zoom factor = 128
  336.  
  337.     xor     eax,eax
  338.     shrd    eax,edx,16
  339.     sar     edx,16
  340.     idiv    ebx
  341.     mov     edx,ecx
  342.     mov     ecx,eax
  343.  
  344.     ret
  345.  
  346. Three2TwoD      ENDP
  347. ;--------------------------------------------------
  348. ;**************************************************
  349.